package com.woniu40.petai_manager.web;

import com.woniu40.petai_manager.entity.GoodsType;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("goodsType")
@Api("二级分类标签")
@CrossOrigin
public class GoodsTypeController {


    @Autowired
    GoodsTypeService goodsTypeService;

    @PostMapping("/add")
    @ApiOperation("新增二级分类")
    public ResponseResult<String> add(@RequestBody GoodsType goodsType){
        ResponseResult<String> result = new ResponseResult<>();
        int i = goodsTypeService.addGt(goodsType);
        result.setCode(200);
        result.setMessage("添加二级分类成功！");
        return result;
    }


    @GetMapping("/findById")
    @ApiOperation("根据ID查询二级分类对象")
    public ResponseResult<GoodsType> toModify(Integer goodsTypeId){
        ResponseResult<GoodsType> result = new ResponseResult<>();
        GoodsType goodsType = goodsTypeService.findById(goodsTypeId);
        result.setCode(200);
        result.setMessage("查询二级分类成功！");
        result.setData(goodsType);
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation("更新二级分类信息")
    public ResponseResult<String> modify(@RequestBody GoodsType goodsType){
        ResponseResult<String> result = new ResponseResult<>();
        int i = goodsTypeService.modifyGt(goodsType);
        result.setCode(200);
        result.setMessage("更新成功！");
        return result;
    }

    @DeleteMapping("/removeAll")
    @ApiOperation("根据id删除二级分类(可批量)")
    public ResponseResult<String> removeGoods(String ids){
        ResponseResult<String> result = new ResponseResult<>();
        if(ids.contains(",")){
            List<Integer> idList = new ArrayList<>();
            String[] idsStr = ids.split(",");
            for (String idStr : idsStr) {
                idList.add(Integer.valueOf(idStr));
            }
            int i = goodsTypeService.removeByIds(idList);
        }else{
            Integer id = Integer.valueOf(ids);
            int i = goodsTypeService.removePro(id);
        }
        result.setCode(200);
        result.setMessage("删除二级分类成功！");
        return result;
    }
}
